package com.hdwx.weike.controllers;

import com.hdwx.weike.service.IUserService;
import com.hdwx.weike.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created by hdwx on 2016/12/23.
 */
@WebServlet(urlPatterns = "/login.do")
public class LoginController extends BaseController{
    IUserService service=new UserServiceImpl();
    @Override
    public void todo(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html");
        String account = req.getParameter("account");
        String pwd=req.getParameter("password");
        String code_imgStr=req.getParameter("code_img");
        System.out.println("验证码:"+code_imgStr);
        String str=req.getParameter("code");
//        int code=0;
//        if(str!=null){
//            try{
//                code=Integer.valueOf(str);
//            }catch (Exception e){}
//        }
        boolean login = service.login(account, pwd);
        if(login){
            if(str.equals(code_imgStr)){
                //登陆成功，创建session对象,跳转到首页
                HttpSession session = req.getSession();
                session.setAttribute("account",account);
                resp.sendRedirect("home.jsp");
            }else{
                //登录失败，跳转到登录界面

                resp.sendRedirect("login.jsp?error=2");
            }

        }else{
            //登录失败，跳转到登录界面

            resp.sendRedirect("login.jsp?error=1");
        }

    }
}
